#include "PageLib.hh"
#include <iostream>
#include "WebPage.hh"
#include "tinyxml2.h"
#include <cerrno>
#include <cstdlib>
#include <cstring>
#include "DirScanner.hh"
#include <regex>
#include<fstream>
#include<unistd.h>

using namespace tinyxml2;
using namespace std;

    


//class PageLib;

PageLib::PageLib(DirScanner &dirScanner)
:_dirScanner(dirScanner)
{
}



void PageLib::create()
{
    //_dirScanner;
    //cout<<_dirScanner.files().size()<<endl;
    //_dirScanner.get();
    int id = 1;
    int j = 0;
    int offsetBegin = 0;
    int lenth = 0;

    //string tem=_dirScanner._files[j];
    XMLDocument doc;
    //doc.LoadFile(_dirScanner._files[j].c_str());
    //doc.ErrorID();
    //std::ofstream outFile("../data/tipepage.lib");
    string title,link,content;

#if 1
    WebPage webpage;
    while(j<_dirScanner.files().size())
    {
    //cout<<_dirScanner.files().size()<<endl;
	doc.LoadFile(_dirScanner.files()[j].c_str());
    doc.ErrorID();

    XMLElement* channelElement = doc.FirstChildElement("rss")->FirstChildElement("channel");

    XMLElement* itemElement = channelElement->FirstChildElement("item");
  
    XMLElement* titleElement;
    XMLElement* linkElement;
    XMLElement* contentElement;


        while (itemElement != nullptr) {
            titleElement = itemElement->FirstChildElement("title");
            if (titleElement) {
                title = titleElement->GetText();
                //webpage._docTitle=title;
            }

            linkElement = itemElement->FirstChildElement("link");
            if (linkElement) {
                link = linkElement->GetText();
                //webpage._docUrl=link;
            }

            {
            contentElement = itemElement->FirstChildElement("content");
            if (contentElement) {
                content = contentElement->GetText();
                std::regex tagRegex("<[^>]*>");
                content = std::regex_replace(content , tagRegex, "");
                //webpage._docContent=content;
            }
            else{
                contentElement = itemElement->FirstChildElement("description");
                if (contentElement) {
                    content = contentElement->GetText();
                    std::regex tagRegex("<[^>]*>");
                    content = std::regex_replace(content , tagRegex, "");
                    //webpage._docContent=content;
                }
            }
            }
            //webpage._docId=id;

            stringstream item;
            item<< "<doc><docid>" << id << "</docid><title>" << title
               << "</title><link>" << link << "</link><content>"
               << content<< "</content></doc>";
               //cout<<i+1<<"    "<<content<<endl;
            string txt = item.str();
    //outFile << txt<< std::endl;
            _files.push_back(txt);
            //创建网页偏移库
            #if 0
            {
                lenth=strlen(txt.c_str());
                auto mypair=make_pair(offsetBegin,lenth);
                _offsetLib.insert(make_pair(i,mypair));
                offsetBegin=lenth+offsetBegin+1;
                #if 0
                //cout<<lenth<<endl;
                //cout<<offsetBegin<<endl;
                //if()
                cout<<i<<"    "<<_offsetLib.find(i)->second.first<<"    "<<_offsetLib.find(i)->second.second<<endl;
                //cout<<_offsetLib.find(i)->second<<endl;
                #endif
            }
            #endif
            #if 0 
            cout<<"<doc><docid>" << i<< "</docid><title>" << title
               << "</title><link>" << link << "</link><content>"
               << content<< "</content></doc>"<<endl;

            #endif
            //_files.push_back(txt);
            itemElement= itemElement->NextSiblingElement("item");
            id++;
        }
        j++;
    }
//outFile.close();
#endif

}
vector<string>& PageLib::getFiles()//获取网页容器
{
    return _files;
}
#if 0
map<int,pair<int,int>>& PageLib::getOffsetLib()//获取偏移库信息
{
    return _offsetLib;
}
#endif







#if 0
int main()
{
    DirScanner _dirScanner;
    _dirScanner();
    PageLib page(_dirScanner);
    page.create();

}


#endif